package cn.holy.app.xcddt.ui.activity.archive

import android.content.ClipboardManager
import android.content.Context
import android.media.MediaPlayer
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.app.GlideApp
import cn.holy.app.xcddt.domain.RecordBean
import cn.holy.app.xcddt.domain.RecordCheckBean
import cn.holy.app.xcddt.domain.RecordGroup
import cn.holy.app.xcddt.ui.activity.base.BaseActivity
import cn.holy.app.xcddt.utils.HDateUtils
import cn.holy.app.xcddt.utils.toImageUrl
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.QMUIItemViewsAdapter
import com.yanzhenjie.album.Album
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.activity_list.*


/**
 * Created by xionghao on 2018/3/1.
 */
@Route(path = "/app/archives/group_list")
class ArchiveListActivity : BaseActivity() {


    private val checks: ArrayList<RecordCheckBean> = ArrayList()
    private var commonAdapter: CommonAdapter<RecordBean>? = null
    private var mediaPlayer: MediaPlayer? = null
    private lateinit var clipboardManager: ClipboardManager

    override fun initData() {
        val group = intent.getSerializableExtra("group") as RecordGroup
        clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerView.layoutManager = layoutManager
        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        if (group.list != null) {
            commonAdapter = object : CommonAdapter<RecordBean>(this, R.layout.layout_archive_item, group.list) {

                override fun convert(holder: ViewHolder, recordBean: RecordBean, position: Int) {

                    holder.getView<TextView>(R.id.tv_record_no).text = "案卷编号：" + recordBean.record_no
                    holder.getView<TextView>(R.id.tv_message).text = recordBean.message
                    holder.getView<TextView>(R.id.tv_address).text = recordBean.address
                    holder.getView<TextView>(R.id.tv_status).text = recordBean.status_label
                    holder.getView<TextView>(R.id.tv_record_people).text = recordBean.record_people
                    holder.getView<TextView>(R.id.tv_record_12319).text = "12319编号：" + recordBean.task_num
                    holder.getView<TextView>(R.id.tv_record_group).text = "第" + (recordBean.record_num) + "次巡查"
                    holder.getView<TextView>(R.id.tv_time).text = "记录时间：" + HDateUtils.dateToStrLong(recordBean.created_at)
                    holder.getView<TextView>(R.id.tv_message).setOnLongClickListener {
                        clipboardManager.text = recordBean.message
                        showToast("成功复制案卷信息到剪切板")
                        return@setOnLongClickListener true
                    }
                    holder.getView<TextView>(R.id.tv_record_12319).setOnLongClickListener {
                        clipboardManager.text = recordBean.task_num
                        showToast("成功复制12319编号到剪切板")
                        return@setOnLongClickListener true
                    }
                    holder.getView<TextView>(R.id.tv_record_no).setOnLongClickListener {
                        clipboardManager.text = recordBean.record_no
                        showToast("成功复制案卷编号到剪切板")

                        return@setOnLongClickListener true
                    }
                }
            }
            commonAdapter?.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
                override fun onItemClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int) {
                    if (group.list != null) {
                        val item = group.list!![position]
                        ARouter.getInstance().build("/app/archives/detail").withSerializable("archive", item)
                                .navigation()
                    }
                }

                override fun onItemLongClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int): Boolean {
                    return true
                }

            })
        }

        recyclerView.adapter = commonAdapter
    }

    override fun initView() {
        App.appComponent.inject(this)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        top_bar.setTitle("巡查列表").setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        top_bar.addLeftImageButton(R.mipmap.ic_back, R.id.action_close).setOnClickListener({ finish() })
//        pageAdapter = TabPager(supportFragmentManager)
//        viewpager.adapter = pageAdapter
    }


    override fun initLayout() {
        setContentView(R.layout.activity_list)
    }

}
